package com.wc.alorithm_blue_bridge._思维.Q5106;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/11/9 20:36
 * @description
 * 质数签到题
 * https://www.lanqiao.cn/problems/4583/learning/?page=1&first_category_id=1
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long num = sc.nextLong();
        Main main = new Main();
        System.out.println(main.maxPrime(num, new HashMap<Long, Long>()));
    }

    /**
     * 求出最大值质数
     * @param num 传入的值
     * @param map 防止重复计算
     * @return 最大质数
     */
    public long maxPrime(long num, HashMap<Long, Long> map) {
        if (isPrime(num)) {
            return num;
        }
        if (num < 10) {
            return -1;
        }
        // 表示去掉右边的数
        long left = num / 10;
        long maxLeftPrime;

        if (map.containsKey(left)) {
            maxLeftPrime = map.get(left);
        } else {
            maxLeftPrime = maxPrime(left, map);
            map.put(left, maxLeftPrime);
        }

        // 表示去掉左边的数
        long right = num % (long) Math.pow(10, String.valueOf(num).length() - 1);
        long maxRightPrime;
        if (map.containsKey(right)) {
            maxRightPrime = map.get(right);
        } else {
            maxRightPrime = maxPrime(right, map);
            map.put(right, maxRightPrime);
        }

        return Math.max(maxLeftPrime, maxRightPrime);
    }

    /**
     * 判断num是否为质数
     * @param num
     * @return
     */
    public boolean isPrime(long num) {
        if (num <= 1) {
            return false;
        }
        long mid = (long) Math.sqrt(num);
        for (int i = 2; i <= mid; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}
